package threadLearn;

import java.util.Random;

public class demon5 {

    public static void main1(String[] args) throws InterruptedException {
        Thread[] threads=new Thread[20];

        for ( int i = 0; i < 20; i++) {

            final int n=i;
            threads[i]=new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("线程"+threads[n].getName()+" "+n);
                }
            });
        }
        for(Thread x:threads){
            x.start();
            //x.join();
        }
        for(Thread x:threads){
            x.join();
        }
        System.out.println("ok");
    }

    public static void main(String[] args) throws InterruptedException {
        Operator operator=new Operator();
        Random random=new Random();
        final int[] arr=new int[10000000];
        for (int i = 0; i < 100; i++) {
            int a=random.nextInt(1,100)+1;
            arr[i]=a;
        }
//        for(int i=0;i<100;i++){
//            System.out.println(arr[i]);
//        }

        Thread thread=new Thread(()->{
            int sum1=0;
            for (int i = 0; i < 100; i++) {
                if(arr[i]%2==0){

                    System.out.println(arr[i]);
                    operator.getEvenNumber(arr[i]);
                }
            }

        });
        Thread thread1=new Thread(()->{
            int sum2=0;
            for (int i = 0; i < 100; i++) {
                if(arr[i]%2!=0){
                    operator.getOddNumber(arr[i]);
                }
            }

        });
        thread1.start();
        thread.start();
        thread1.join();
        thread.join();
        System.out.println(operator.result());
    }
}
class Operator{
    int oddNumber;
    int evenNumber;

    public void getOddNumber(int a) {
        oddNumber+=a;

    }
     public void getEvenNumber(int b) {
        evenNumber+=b;

    }

//    public void setEvenNumber(int evenNumber) {
//        this.evenNumber = evenNumber;
//    }
    public int result(){
//        System.out.println(oddNumber);
//        System.out.println(evenNumber);
        return oddNumber+evenNumber;
    }
}
